package com.taiji.dianthus.dao.sms;

import com.taiji.dianthus.domain.sms.SmsGroupUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @InterfaceName SmsGroupRepository
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Repository
public interface SmsGroupUserRepository extends JpaRepository<SmsGroupUser, String>,
        JpaSpecificationExecutor<SmsGroupUser> {


    @Query("select count(t) from SmsGroupUser t where t.enableFlag=1 and  t.groupId=:id")
    Integer countByGroupId(@Param("id") String id);

    @Transactional
    @Modifying
    @Query("update SmsGroupUser t set t.enableFlag=0 where t.id=:id")
    void updateFlag(@Param("id") String id);

    @Transactional
    @Modifying
    @Query("update SmsGroupUser t set t.enableFlag=0 where t.id in (:ids)")
    void updateFlagArrays(@Param("ids") List<String> list);

    @Query("select t from SmsGroupUser t where t.enableFlag=1 and  t.groupId=:groupId")
    List<SmsGroupUser> getAllUserListByGroupIdNotPage(@Param("groupId") String groupId);

    @Query("select t from SmsGroupUser t where t.enableFlag=1 and  t.groupId=:groupId and t.userPhone=:userPhone")
    List<SmsGroupUser> checkUserPhoneByGroup(@Param("groupId") String groupId, @Param("userPhone") String userPhone);

    @Query("select t from SmsGroupUser t where t.enableFlag=1 and  t.groupId=:groupId and t.userFlag=0")
    List<SmsGroupUser> getAllUserListByGroupIdBooks(@Param("groupId")String groupId);
}
